Trò chơi Tic-Tac-Toe, game đánh caro full source code
53.496 lượt xem;
- InRoomChat.cs
- UtilityScripts /
- Photon Unity Networking /
- Assets /
- project /
1 using System.Collections.Generic;
2 using UnityEngine;
3 using System.Collections;
4
5 [RequireComponent(typeof(PhotonView))]
6 public class InRoomChat : Photon.MonoBehaviour
7 {
8 public Rect GuiRect = new Rect(0,0, 250,300);
9 public bool IsVisible = true;
10 public bool AlignBottom = false;
11 public List<string> messages = new List<string>();
12 private string inputLine = "";
13 private Vector2 scrollPos = Vector2.zero;
14
15 public static readonly string ChatRPC = "Chat";
16
17 public void Start()
18 {
19 if (this.AlignBottom)
20 {
21 this.GuiRect.y = Screen.height - this.GuiRect.height;
22 }
23 }
24
25 public void OnGUI()
26 {
27 if (!this.IsVisible || PhotonNetwork.connectionStateDetailed != PeerState.Joined)
28 {
29 return;
30 }
31
32 if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
33 {
34 if (!string.IsNullOrEmpty(this.inputLine))
35 {
36 this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
37 this.inputLine = "";
38 GUI.FocusControl("");
39 return; // printing the now modified list would result in an error. to avoid this, we just skip this single frame
40 }
41 else
42 {
43 GUI.FocusControl("ChatInput");
44 }
45 }
46
47 GUI.SetNextControlName("");
48 GUILayout.BeginArea(this.GuiRect);
49
50 scrollPos = GUILayout.BeginScrollView(scrollPos);
51 GUILayout.FlexibleSpace();
52 for (int i = messages.Count - 1; i >= 0; i--)
53 {
54 GUILayout.Label(messages[i]);
55 }
56 GUILayout.EndScrollView();
57
58 GUILayout.BeginHorizontal();
59 GUI.SetNextControlName("ChatInput");
60 inputLine = GUILayout.TextField(inputLine);
61 if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
62 {
63 this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
64 this.inputLine = "";
65 GUI.FocusControl("");
66 }
67 GUILayout.EndHorizontal();
68 GUILayout.EndArea();
69 }
70
71 [RPC]
72 public void Chat(string newLine, PhotonMessageInfo mi)
73 {
74 string senderName = "anonymous";
75
76 if (mi != null && mi.sender != null)
77 {
78 if (!string.IsNullOrEmpty(mi.sender.name))
79 {
80 senderName = mi.sender.name;
81 }
82 else
83 {
84 senderName = "player " + mi.sender.ID;
85 }
86 }
87
88 this.messages.Add(senderName +": " + newLine);
89 }
90
91 public void AddLine(string newLine)
92 {
93 this.messages.Add(newLine);
94 }
95 }
2 using UnityEngine;
3 using System.Collections;
4
5 [RequireComponent(typeof(PhotonView))]
6 public class InRoomChat : Photon.MonoBehaviour
7 {
8 public Rect GuiRect = new Rect(0,0, 250,300);
9 public bool IsVisible = true;
10 public bool AlignBottom = false;
11 public List<string> messages = new List<string>();
12 private string inputLine = "";
13 private Vector2 scrollPos = Vector2.zero;
14
15 public static readonly string ChatRPC = "Chat";
16
17 public void Start()
18 {
19 if (this.AlignBottom)
20 {
21 this.GuiRect.y = Screen.height - this.GuiRect.height;
22 }
23 }
24
25 public void OnGUI()
26 {
27 if (!this.IsVisible || PhotonNetwork.connectionStateDetailed != PeerState.Joined)
28 {
29 return;
30 }
31
32 if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
33 {
34 if (!string.IsNullOrEmpty(this.inputLine))
35 {
36 this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
37 this.inputLine = "";
38 GUI.FocusControl("");
39 return; // printing the now modified list would result in an error. to avoid this, we just skip this single frame
40 }
41 else
42 {
43 GUI.FocusControl("ChatInput");
44 }
45 }
46
47 GUI.SetNextControlName("");
48 GUILayout.BeginArea(this.GuiRect);
49
50 scrollPos = GUILayout.BeginScrollView(scrollPos);
51 GUILayout.FlexibleSpace();
52 for (int i = messages.Count - 1; i >= 0; i--)
53 {
54 GUILayout.Label(messages[i]);
55 }
56 GUILayout.EndScrollView();
57
58 GUILayout.BeginHorizontal();
59 GUI.SetNextControlName("ChatInput");
60 inputLine = GUILayout.TextField(inputLine);
61 if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
62 {
63 this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
64 this.inputLine = "";
65 GUI.FocusControl("");
66 }
67 GUILayout.EndHorizontal();
68 GUILayout.EndArea();
69 }
70
71 [RPC]
72 public void Chat(string newLine, PhotonMessageInfo mi)
73 {
74 string senderName = "anonymous";
75
76 if (mi != null && mi.sender != null)
77 {
78 if (!string.IsNullOrEmpty(mi.sender.name))
79 {
80 senderName = mi.sender.name;
81 }
82 else
83 {
84 senderName = "player " + mi.sender.ID;
85 }
86 }
87
88 this.messages.Add(senderName +": " + newLine);
89 }
90
91 public void AddLine(string newLine)
92 {
93 this.messages.Add(newLine);
94 }
95 }
return; printing the now modified list would result in an error. to avoid this, we just skip this single frame